import Vue from 'vue';

Vue.filter('numberToUpperCase', function (value) {
    if (value || value == 0)
        return String.fromCharCode(value + 65);
});

Vue.filter('mobileHidden', function (value) {
    if (value)
        return value.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
});

// YYYY-MM-DD hh-mm
Vue.filter('timeToYMDHM', function (value) {
    if (value){
        let time = new Date(value)
            ,year = time.getUTCFullYear()
            ,month = time.getUTCMonth()+1
            ,day = time.getUTCDate()
            ,hour = time.getUTCHours()
            ,min = time.getUTCMinutes();
        if(month<10) month = '0'+month;
        if(min<10) min = '0'+min;
        return year+'-'+month+'-'+day+' '+hour+':'+min;
    }
});

// YYYY-MM-DD hh-mm-ss
Vue.filter('timeToYMDHMS', function (value) {
    if (value){
        let time = new Date(value)
            ,year = time.getUTCFullYear()
            ,month = time.getUTCMonth()+1
            ,day = time.getUTCDate()
            ,hour = time.getUTCHours()
            ,min = time.getUTCMinutes()
            ,second = time.getUTCSeconds();
        if(month<10) month = '0'+month;
        if(min<10) min = '0'+min;
        return year+'-'+month+'-'+day+' '+hour+':'+min+':'+second;
    }
});

// YYYY-MM-DD(w) hh-mm-ss
Vue.filter('timeToYMDWHMS', function (value) {
    if (value){
        let week=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
        let time = new Date(value)
            ,year = time.getUTCFullYear()
            ,month = time.getUTCMonth()+1
            ,day = time.getUTCDate()
            ,weekday = week[time.getDay()]
            ,hour = time.getUTCHours()
            ,min = time.getUTCMinutes()
            ,second = time.getUTCSeconds();
        if(month<10) month = '0'+month;
        if(min<10) min = '0'+min;
        return year+'-'+month+'-'+day+'（'+weekday+'） '+hour+':'+min+':'+second;
    }
});

// YYYY-MM-DD
Vue.filter('timeToYMD', function (value) {
    if (value){
        const time = new Date(value);
        const year = time.getUTCFullYear();
        let month = time.getUTCMonth()+1;
        if(month<10) month = '0'+month;
        const day = time.getUTCDate();
        return year+'-'+month+'-'+day;
    }
});

// hh-mm
Vue.filter('timeToHM', function (value) {
    if (value){
        const time = new Date(value);
        const hour = time.getUTCHours();
        let min = time.getUTCMinutes();
        if(min<10) min = '0'+min;
        return hour+':'+min;
    }
});

// hh-mm-ss
Vue.filter('timeToHMS', function (value) {
    if (value){
        let time = new Date(value);
        let hour = time.getUTCHours();
        let min = time.getUTCMinutes();
        let seconds = time.getUTCSeconds();
        if(hour<10) hour = '0'+hour;
        if(min<10) min = '0'+min;
        if(seconds<10) seconds = '0'+seconds;
        return hour+':'+min+':'+seconds;
    }
});
// 时间转换 Y年m月d日
Vue.filter('toYmdStr', function (value){
    let dateTime = new Date(value);
    let Year = dateTime.getFullYear();
    let Month = (dateTime.getMonth()+1);
    Month = Month < 10 ? ('0' + Month) : Month;
    let Day = dateTime.getDate();
    Day = Day < 10 ? ('0' + Day) : Day;
    return Year + '年' + Month + '月' + Day + '日';
});
